package org.jay.angla.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.jay.angla.adapter.SpinArrayAdapter;
import org.jay.angla.onlab.OnlabApplication;
import org.jay.angla.onlab.R;
import org.jay.angla.onlab.R.array;
import org.jay.angla.onlab.R.id;
import org.jay.angla.onlab.R.layout;
import org.jay.angla.onlab.R.string;
import org.jay.angla.utils.Contants;
import org.jay.angla.utils.SharePreferenceUtil;
import org.jay.angla.utils.ToastShow;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

@EActivity(R.layout.activity_ws_setting)
public class WSSettingActivity extends Activity {

	@ViewById(R.id.tv_y_up_unit)
	TextView mUpUnitTextView;
	@ViewById(R.id.et_y_uplimit)
	EditText mUpLimitEditText;
	@ViewById(R.id.tv_y_down_unit)
	TextView mDownUnitTextView;
	@ViewById(R.id.et_y_downlimit)
	EditText mDownLimitEditText;
	@ViewById(R.id.rg_test_mode)
	RadioGroup mTestModeRadioGroup;
	@ViewById(R.id.rb_test_mode_abs)
	RadioButton mAbsRadioButton;
	@ViewById(R.id.rb_test_mode_trans)
	RadioButton mTransRadioButton;
	@ViewById(R.id.rb_test_mode_energy)
	RadioButton mEnergyRadioButton;

	@ViewById(R.id.rg_scan_speed)
	RadioGroup mScanSpeedRadioGroup;
	@ViewById(R.id.rb_scan_fast)
	RadioButton mScanFastButton;
	@ViewById(R.id.rb_scan_stand)
	RadioButton mScanStandRadioButton;
	@ViewById(R.id.rb_scan_slow)
	RadioButton mScanSlowRadioButton;

	@ViewById(R.id.btn_ws_sure)
	Button mSureButton;
	@ViewById(R.id.btn_ws_cancel)
	Button mCancelButton;
	@ViewById(R.id.et_start_wl)
	EditText mStartWlEditText;
	@ViewById(R.id.et_end_wl)
	EditText mEndWlEditText;
	@ViewById(R.id.sp_wl_interval)
	Spinner mWlIntervalSpinner;
	@ViewById(R.id.sp_gain)
	Spinner mGainSpinner;
	@ViewById(R.id.sp_filter)
	Spinner mFilterSpinner;
	@ViewById(R.id.sp_light_source)
	Spinner mLightSourceSpinner;
	@ViewById(R.id.tv_gain)
	TextView mGainTextView;
	@ViewById(R.id.tv_filter)
	TextView mFilterTextView;
	@ViewById(R.id.tv_light_source)
	TextView mLightSourceTextView;

	private ToastShow mToastShow;

	public static final int TEST_MODE_ABS = 0;
	public static final int TEST_MODE_TRANS = 1;
	public static final int TEST_MODE_ENERGY = 2;

	public static final int SCAN_SPEED_FAST = 0;
	public static final int SCAN_SPEED_STAND = 1;
	public static final int SCAN_SPEED_SLOW = 2;

	private float mUpLimit;
	private float mDownLimit;
	private int mTestMode;
	private float mStartWavelength;
	private float mEndWavelength;
	private float mWavelengthInterval;

	private int mScanSpeed = SCAN_SPEED_STAND;

	@AfterViews
	public void init_ui() {
		String[] mWlStrings = getResources()
				.getStringArray(R.array.wl_interval);
		ArrayAdapter<String> adapter = new SpinArrayAdapter(this, mWlStrings);
		mWlIntervalSpinner.setAdapter(adapter);
		mWlIntervalSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// TODO Auto-generated method stub
						String[] value = getResources().getStringArray(
								R.array.wl_interval);
						mWavelengthInterval = Float.parseFloat(value[pos]);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		String[] tempString = getResources().getStringArray(R.array.gain);
		adapter = new SpinArrayAdapter(this, tempString);
		mGainSpinner.setAdapter(adapter);
		tempString = getResources().getStringArray(R.array.filter);
		adapter = new SpinArrayAdapter(this, tempString);
		mFilterSpinner.setAdapter(adapter);
		tempString = getResources().getStringArray(R.array.light_source);
		adapter = new SpinArrayAdapter(this, tempString);
		mLightSourceSpinner.setAdapter(adapter);

		mTestModeRadioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int arg1) {
						// TODO Auto-generated method stub
						int id = group.getCheckedRadioButtonId();
						if (id == R.id.rb_test_mode_abs) {
							mTestMode = TEST_MODE_ABS;
							mUpUnitTextView.setText("Abs");
							mDownUnitTextView.setText("Abs");
							setGainEnable(false);
						} else if (id == R.id.rb_test_mode_trans) {
							mTestMode = TEST_MODE_TRANS;
							mUpUnitTextView.setText("%T");
							mDownUnitTextView.setText("%T");
							setGainEnable(false);
						} else if (id == R.id.rb_test_mode_energy) {
							mTestMode = TEST_MODE_ENERGY;
							mUpUnitTextView.setText("");
							mDownUnitTextView.setText("");
							setGainEnable(true);
						}
					}
				});

		mScanSpeedRadioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						mScanSpeed = group.getCheckedRadioButtonId();
					}
				});
		mSureButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if ((mUpLimitEditText.getEditableText().toString().length() < 1)
						|| (mDownLimitEditText.getEditableText().toString()
								.length() < 1)
						|| (mStartWlEditText.getEditableText().toString()
								.length() < 1)
						|| (mEndWlEditText.getEditableText().toString()
								.length() < 1)) {
					mToastShow
							.toastShow(getString(R.string.input_correct_param));
					return;
				}
				saveSp();
				finish();
			}
		});
		mCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		loadDefaultSetting();
	}

	private void saveSp() {
		SharePreferenceUtil sp = OnlabApplication.getInstance().getSpUtil();
		mUpLimit = Float.parseFloat(mUpLimitEditText.getEditableText()
				.toString());
		mDownLimit = Float.parseFloat(mDownLimitEditText.getEditableText()
				.toString());
		mStartWavelength = Float.parseFloat(mStartWlEditText.getEditableText()
				.toString());
		mEndWavelength = Float.parseFloat(mEndWlEditText.getEditableText()
				.toString());

		sp.setWSTestMode(mTestMode);
		sp.setWSUpLimit(mUpLimit);
		sp.setWSDownLimit(mDownLimit);
		sp.setWSStartWl(mStartWavelength);
		sp.setWSEndWl(mEndWavelength);
		sp.setWSInterval(mWavelengthInterval);
	}

	private void loadDefaultSetting() {
		SharePreferenceUtil sp = OnlabApplication.getInstance().getSpUtil();
		mTestMode = sp.getWSTestMode();
		mUpLimit = sp.getWSUpLimit();
		mDownLimit = sp.getWSDownLimit();
		mStartWavelength = sp.getWSStartWl();
		mEndWavelength = sp.getWSEndWl();
		mWavelengthInterval = sp.getWSInterval();

		mScanStandRadioButton.setChecked(true);

		setGainEnable(false);
		if (mTestMode == TEST_MODE_ABS) {
			mAbsRadioButton.setChecked(true);
			mTransRadioButton.setChecked(false);
			mEnergyRadioButton.setChecked(false);
		} else if (mTestMode == TEST_MODE_TRANS) {
			mAbsRadioButton.setChecked(false);
			mTransRadioButton.setChecked(true);
			mEnergyRadioButton.setChecked(false);
		} else if (mTestMode == TEST_MODE_ENERGY) {
			mAbsRadioButton.setChecked(false);
			mTransRadioButton.setChecked(false);
			mEnergyRadioButton.setChecked(true);
			setGainEnable(true);
		}
		mUpLimitEditText.setText("" + mUpLimit);
		mDownLimitEditText.setText("" + mDownLimit);
		mStartWlEditText.setText("" + mStartWavelength);
		mEndWlEditText.setText("" + mEndWavelength);
		String[] intervalStrings = getResources().getStringArray(
				R.array.wl_interval);
		int index = 0;
		for (int i = 0; i < intervalStrings.length; i++) {
			if (Float.parseFloat(intervalStrings[i]) == mWavelengthInterval) {
				index = i;
				break;
			}
		}
		if (index >= intervalStrings.length) {
			mWlIntervalSpinner.setSelection(0);
		} else {
			mWlIntervalSpinner.setSelection(index);
		}
	}

	private void setGainEnable(boolean enable) {
		mGainTextView.setEnabled(enable);
		mGainSpinner.setEnabled(enable);
		mFilterSpinner.setEnabled(enable);
		mFilterTextView.setEnabled(enable);
		mLightSourceSpinner.setEnabled(enable);
		mLightSourceTextView.setEnabled(enable);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		mToastShow = new ToastShow(this);
	}
}
